﻿using System;
using System.Diagnostics.Contracts;
using MathService.TypeDesigners;

namespace MathService.Attributes
{
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public sealed class TypeDesignerAttribute : Attribute
    {
        private readonly Type f_DesignerType;

        public ITypeDesigner Designer
        {
            get { return Activator.CreateInstance(DesignerType) as ITypeDesigner; }
        }

        public Type DesignerType { get { return f_DesignerType; } }

        public TypeDesignerAttribute(Type DesignerType)
        {
            Contract.Requires(DesignerType != null);
            Contract.Requires(DesignerType.FindInterfaces((t, m) => t == typeof(ITypeDesigner), null).Length != 0);

            var lv_Interface = DesignerType.GetInterface(typeof(ITypeDesigner).Name);
            if(lv_Interface == null || lv_Interface != typeof(ITypeDesigner))
                throw new ArgumentException("Дизайнер не наследует интерфейс " +
                    typeof(ITypeDesigner).FullName);
            f_DesignerType = DesignerType;
        }
    }
}
